package com.cw.jeeyt.controller.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.cw.jeeyt.service.bo.api.ApiFacadeGenerateConfigBo;
import com.cw.jeeyt.api.dto.facadeExcel.ApiGenerateFacadeExcelDto;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.service.intf.api.ApiFacadeGenerateConfigService;
import com.cw.jeeyt.service.intf.api.ApiGenerateFacadeExcelService;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.lang.common.utils.CollectionUtil;

@Controller
@RequestMapping("/apiFacade")
public class ApiGenerateFacadeExcelController {
    @Autowired
    private ApiGenerateFacadeExcelService apiGenerateFacadeExcelService;
    @Autowired
    private ApiFacadeGenerateConfigService configService;
    /**
     * 跳转到生成接口excel文档页面
     */
    @RequestMapping("/to-generateForm")
    public ModelAndView toGenerateForm() {
        ModelAndView mv = new ModelAndView("api/apiFacadeExcelGenerate_form");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("PageDict", new PageDictDirective());
        List<ApiFacadeGenerateConfigBo> generateConfigBoList = configService.queryList(new ApiFacadeGenerateConfigBo());
        if(CollectionUtil.isNotEmpty(generateConfigBoList)){
            mv.addObject("facadeGenerateConfig",generateConfigBoList.get(0));
        }else{
            mv.addObject("facadeGenerateConfig",new ApiFacadeGenerateConfigBo());
        }
        return mv;
    }

    /**
     * 生成接口excel文档，并落入数据库
     */
    @RequestMapping("/generateExcel")
    @ResponseBody
    public JsonResultDto generateExcel(ApiGenerateFacadeExcelDto facadeExcelDto) throws Exception {
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            apiGenerateFacadeExcelService.generateFacadeExcel(facadeExcelDto);
            jsonResultDto.setSuccess(true);
            jsonResultDto.setMsg("接口文档生成成功");
        }catch (Exception e){
            jsonResultDto.setMsg("接口文档生成出错：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
